package com.gw.booking.controller.wechat;

import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.common.Util;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.backend.DiscountListDtoOut;
import com.gw.booking.dto.wechat.WCustomerDiscountDtoOut;
import com.gw.booking.service.CustomerDiscountService;
import com.gw.booking.service.DiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Api(tags = {"微信-优惠管理"})
@Controller
@RequestMapping("/wechat/discount")
public class WDiscountController {

    @Autowired
    private CustomerDiscountService customerDiscountService;
    @Autowired
    private DiscountService discountService;

    @ApiOperation(value = "我的优惠列表")
    @RequestMapping(value = "/list.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<PageDto<WCustomerDiscountDtoOut>> list(HttpServletRequest request
            , @ApiParam(value = "使用状态0保存1使用", required = true, allowableValues = "0,1") @RequestParam Integer state
            , @ApiParam(value = "页数", required = true) @RequestParam int page
            , @ApiParam(value = "数量", required = true) @RequestParam int size)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, customerDiscountService.list(Util.getLoginToken(request), state, page, size));
    }

    @ApiOperation(value = "添加优惠")
    @RequestMapping(value = "/{discountId}/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject add(HttpServletRequest request
            , @ApiParam(value = "优惠id", required = true) @PathVariable Integer discountId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        customerDiscountService.txAdd(Util.getLoginToken(request), discountId);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "删除优惠")
    @RequestMapping(value = "/{customerDiscountId}/delete.do", method = {RequestMethod.DELETE})
    @ResponseBody
    public ResultObject delete(HttpServletRequest request
            , @ApiParam(value = "优惠id", required = true) @PathVariable Integer customerDiscountId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        customerDiscountService.txDelete(Util.getLoginToken(request), customerDiscountId);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "商家优惠列表")
    @RequestMapping(value = "/merchantDiscountList.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<List<DiscountListDtoOut>> merchantDiscountList(HttpServletRequest request
            , @ApiParam(value = "商家Id", required = true) @RequestParam Integer merchantId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, discountService.merchantDiscountList(Util.getLoginToken(request), merchantId));
    }

}
